package com.zbu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbu.reggie.common.R;
import com.zbu.reggie.dto.SetmealDto;
import com.zbu.reggie.entity.Category;
import com.zbu.reggie.entity.Setmeal;
import com.zbu.reggie.service.CategoryService;
import com.zbu.reggie.service.SetmealDishService;
import com.zbu.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
/*套餐管理*/
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /*新增套餐
     * 套餐表和套餐菜品关系表*/
    @CacheEvict(value = "setmealCache", allEntries = true)//allEntries清理所有缓存
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /*
     * 套餐信息分页查询*/
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        //分页构造器
        Page<Setmeal> pageinfo = new Page<>(page, pageSize);//没有套餐名称
        Page<SetmealDto> dtoPage = new Page<>();//有

        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper();
        wrapper.like(name != null, Setmeal::getName, name);
        //排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        setmealService.page(pageinfo, wrapper);
        //对象拷贝   protected List<T> records;泛型不一样 不能拷贝这个
        BeanUtils.copyProperties(pageinfo, dtoPage, "records");
        //要实现 List<Setmeal> --->List<SetmealDto>
        List<Setmeal> records = pageinfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //赋值除分类名称以外的其它属性
            BeanUtils.copyProperties(item, setmealDto);
            //获取分类id 查询分类名称
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);//分类名称
            }
            return setmealDto;
        }).toList();
        //dtoPage的所有属性都有值了
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /*停售或起售套餐*/
    @PostMapping("/status/{status}")
    public R<String> updates(@PathVariable Integer status,
                             @RequestParam List<Long> ids) {
        log.info("批量起售或停售 菜品");
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.set(Setmeal::getStatus, status);
        setmealService.update(wrapper);

        return R.success("修改成功");
    }


    /*删除套餐*/
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//allEntries清理所有缓存
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除套餐，ids:{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }


    /**
     * 根据条件查询套餐数据
     */
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 根据套餐id查询套餐中的菜品
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }


}
